package com.exwave.socialgame.model_society.service;

import com.exwave.socialgame.model_user.dao.UserDao;
import com.exwave.socialgame.model_user.entity.User;
import com.exwave.socialgame.model_society.dao.CityteamsDao;
import com.exwave.socialgame.model_society.dao.ResponseDao;
import com.exwave.socialgame.model_society.entity.Cityteams;
import com.exwave.socialgame.model_society.entity.Response;
import com.exwave.socialgame.myutils.WolfGameUtils;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * (Response)表服务实现类
 *
 * @author makejava
 * @since 2021-12-14 18:52:14
 */
@Service("responseService")
public class ResponseServiceImpl implements ResponseService {
    @Resource
    private ResponseDao responseDao;
    @Resource
    private CityteamsDao cityteamsDao;
    @Resource
    private UserDao userDao;

    /**
     * 查询多条数据
     *
     * @param cid
     * @return 返回响应人数的UID List
     */
    @Override
    public List<Integer> queryAllByCid(int cid) {
        List<Response> list = this.responseDao.queryAllByCid(cid);
        WolfGameUtils.isListNull(list, "查询出错，请联系管理员");
        List<Integer> integerList = new ArrayList<>();
        for (Response response : list)
            integerList.add(response.getUid());
        return integerList;
    }

    /**
     * 查询多条数据
     *
     * @param uid
     * @return 返回响应帖子的CID List
     */
    @Override
    public List<Integer> queryAllByUid(int uid) {
        List<Response> list = this.responseDao.queryAllByUid(uid);
        WolfGameUtils.isListNull(list, "查询出错，请联系管理员");
        List<Integer> integerList = new ArrayList<>();
        for (Response response : list)
            integerList.add(response.getCid());
        return integerList;
    }


    /**
     * 新增数据
     *
     * @param response 实例对象
     * @return 实例对象
     */
    @Override
    public Response insert(Response response) {
        //条件判断
        User user = this.userDao.queryById(response.getUid());
        WolfGameUtils.isNullOrEmpty(user, "未找到此用户");
        Cityteams cityteams = this.cityteamsDao.queryById(response.getCid());
        WolfGameUtils.isNullOrEmpty(cityteams, "未找到此事件");
        if (cityteams.getCnum() >= cityteams.getPnum())
            WolfGameUtils.returnFailure("响应失败，人数已满");
//        if (cityteams.getPtime().getTime() < GetCurrentTime.getLongtime())
//            MyReUtil.returnFailure("响应失败，已过期");
        WolfGameUtils.isNotNullOrEmpty(this.responseDao.queryById(response.getUid(), response.getCid()), "已经响应过啦，快去评论联系吧");
        this.responseDao.insert(response);
        //修改帖子当前响应人数
        cityteams.setCnum(cityteams.getCnum() + 1);
        this.cityteamsDao.update(cityteams);

        response = this.responseDao.queryById(response.getUid(), response.getCid());
        WolfGameUtils.isNullOrEmpty(response, "响应失败");
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param uid
     * @param cid
     * @return 是否成功
     */
    @Override
    public boolean delete(int uid, int cid) {
        Response response = this.responseDao.queryById(uid, cid);
        WolfGameUtils.isNullOrEmpty(response, "您没有响应此事件");
        int i = this.responseDao.delete(uid, cid);
        if (i > 0) {
            //修改帖子当前响应人数
            Cityteams cityteams = this.cityteamsDao.queryById(cid);
            cityteams.setCnum(cityteams.getCnum() - 1);
            this.cityteamsDao.update(cityteams);
        }
        return true;
    }
}